package cn.interestingshop.servlet;

import cn.interestingshop.entity.Classify;
import cn.interestingshop.service.ClassifyService;
import cn.interestingshop.service.impl.ClassifyServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/back/ClassifySelectOneServlet")
public class ClassifySelectOneServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.设置编码
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        //2.获取请求参数
        String id = request.getParameter("id");
        //3.调用service进行业务处理
        ClassifyService service=new ClassifyServiceImpl();
        Classify classify = service.selectOne(Integer.parseInt(id));

        if(classify.getType()==2){//当前分类是二级分类的时候
            //查询一级分类
            List<Classify> oneclassifies = service.selectAll(0);
            request.setAttribute("oneclassifies",oneclassifies);
            //一级分类的默认选中
            request.setAttribute("Classify1",classify.getParentId());
        }
        if(classify.getType()==3){//当前分类是三级分类的时候
            //查询一级分类
            List<Classify> oneclassifies = service.selectAll(0);
            request.setAttribute("oneclassifies",oneclassifies);

            //查询二级分类id查询二级分类信息
            Classify classify2 = service.selectOne(classify.getParentId());
            //查询一级分类id查询二级分类信息
            List<Classify> towclassifies = service.selectAll(classify2.getParentId());
            request.setAttribute("twoclassifies",towclassifies);
            //一级分类的默认选中
            request.setAttribute("Classify1",classify2.getParentId());
            //二级分类的默认选中
            request.setAttribute("Classify2",classify.getParentId());
        }

        //4.分析返回结果
        request.setAttribute("classify",classify);
        request.getRequestDispatcher("Member_CategoryUpdate.jsp")
                .forward(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
